<?php

namespace App\Controller\App;

use App\Base\BaseCache;
use App\Base\BaseController4App;
use App\Base\Format;
use App\Entity\User;
use App\Model\LazyCommon\MiniAppModel;
use App\Model\LazyCommon\UserModel;
use App\Model\TopicModel;
use App\Model\UserLikeModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/app")
 */
class UserLikeController extends BaseController4App
{


    /**
     * @route("/like/add",name="AppAddUserLike")
     */
    public function AppAddUserLike()
    {
        if (!$what = $this->getParamRequired("what")) {return $this->json_out(0,"参数缺失");}
        if (!$flag = $this->getParamRequired("flag")) {$flag = 0;}

        $uid = $this->needToBeUser();
        $UserLikeModel = new UserLikeModel($this->getDoctrine());

        if ($UserLike = $UserLikeModel->checkLikes($uid,$what,$flag)) {
            return $this->json_out(1,"已收藏",1);
        }
        $UserLikeModel->add($uid,$what,$flag);
        return $this->json_out(1,"已收藏",1);
    }

    /**
     * @route("/like/del",name="AppDelUserLike")
     */
    public function AppDelUserLike()
    {
        if (!$what = $this->getParamRequired("what")) {return $this->json_out(0,"参数缺失");}
        if (!$flag = $this->getParamRequired("flag")) {$flag = 0;}

        $uid = $this->needToBeUser();
        $UserLikeModel = new UserLikeModel($this->getDoctrine());

        if ($UserLike = $UserLikeModel->checkLikes($uid,$what,$flag)) {
            $this->myEm()->remove($UserLike);
            $this->myEm()->flush();
        }
        return $this->json_out(1,"已取消",0);
    }

    /**
     * @route("/check/like",name="AppCheckUserLike")
     */
    public function AppCheckUserLike()
    {
        if (!$what = $this->getParamRequired("what")) {return $this->json_out(0,"参数缺失");}
        $uid = $this->needToBeUser();
        $UserLikeModel = new UserLikeModel($this->getDoctrine());

        if ($UserLike = $UserLikeModel->checkLikes($uid,$what,0)) {
            $this->data["like"] = true;
        }else{
            $this->data["like"] = false;
        }


        if ($UserLike = $UserLikeModel->checkLikes($uid,$what,1)) {
            $this->data["down"] = true;
        }else{
            $this->data["down"] = false;
        }

        return $this->json_out(1,"已收藏",$this->data);
    }


    /**
     * @route("/get/like",name="AppGetUserLike")
     */
    public function AppGetUserLike()
    {
        $uid = $this->needToBeUser();
        $UserLikeModel = new UserLikeModel($this->getDoctrine());
        $data = $UserLikeModel->getByUserIdInArray($uid,0);
        return $this->json_out(1,"获取成功",$data);
    }

}
